<?php
/**
 * 设置了服务亮点的设计师控制器
 */
namespace Beerus\Modules\Designer\Controllers;

use App\Http\Controllers\Controller;
use Doraemon\model\Designer;
use Doraemon\model\Designer\DesignerSpot;
use Doraemon\model\DesignerAuth;
use Doraemon\model\DesignerFinder\VirtualDialogue;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Widgets\Form;
use Illuminate\Http\Request;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use App\Pockets\GridDataModel as pGridDataModel;
use Encore\Admin\Widgets\Tab;
use Illuminate\Support\MessageBag;
use Doraemon\model\Member as mMember;

class SpotDesignerController extends Controller
{

    const PAGE_SIZE = 10;

    public function __construct()
    {
        parent::__construct();
        Admin::script('$(".pjax-container").attr("id","pjax-container");');
    }

    // 设计师列表
    public function index(Request $request, Content $content)
    {
        $page_size = $request->input("per_page", self::PAGE_SIZE);
        $page = $request->input("page", 1);

        $total = Designer::getCntByQuery($request['uid'], $request['spot_id']);
        $lists = Designer::getListByQuery($request['uid'], $request['spot_id'], $page, $page_size);

        $gridModel = new pGridDataModel($lists, $total, [
            'perPage' => $page_size,
        ]);

        $gridList = Admin::grid(
            $gridModel,
            function (Grid $grid) use ($gridModel) {
                $grid->setActionClass(DropdownActions::class);
                $grid->paginate($gridModel->getPerPage());
                $grid->disableExport();
                $grid->disableRowSelector();
                $grid->disableActions();
                $grid->disableCreateButton();

                $grid->filter(function (Grid\Filter $filter) {
                    $filter->disableIdFilter();
                    $filter->column(1 / 2, function ($filter) {
                        $filter->equal('uid', 'UID')->placeholder('请输入设计师UID');
                    });
                    $filter->column(1 / 2, function ($filter) {
                        $all_spot = DesignerSpot::getAllNormalSpot();
                        $spot_key_arr = array_combine(
                            array_column($all_spot, 'spot_id'),
                            array_column($all_spot, 'spot_name')
                        );
                        $filter->equal('spot_id', '亮点名称')->select($spot_key_arr);
                    });
                    $filter->expand();
                });

                $grid->column('uid', '设计师UID');
                $grid->column('nick', '设计师昵称')->display(function() {
                    return mMember::get('nick', ['uid' => $this->uid]);
                });
                $grid->column('service_spot', '服务亮点')->display(function() {
                    $service_spot = [];
                    if ($this->service_spot) {
                        $service_spot_ids_arr = explode(',', $this->service_spot);
                        foreach ($service_spot_ids_arr as $spot_id) {
                            $spot_name = DesignerSpot::get('spot_name', ['spot_id' => $spot_id]);
                            if ($spot_name) {
                                $service_spot[] = $spot_name;
                            }
                        }
                    }
                    return implode(',', $service_spot);
                });
            }
        );

        $tab = new Tab();
        $tab->add('设计师设置列表', $gridList->render());
        $content->row($tab);
        return $content
            ->header('设计师设置列表')
            ->description(' ')
            ->breadcrumb(['text' => '设计师设置列表']);
    }

    // 新增/修改奖项信息
    public function edit(Request $request, Content $content, $spot_id = 0) {
        $request = $request->all();

        if (!$spot_id) {
            $is_create = true;
            $header = '新增';
        } else {
            $is_create = false;
            $header = '修改';
        }

        $tab = new Tab();
        $tab->add('服务亮点信息', self::editAction($request, $spot_id, $is_create));
        return $content->header($header)
            ->breadcrumb([
                'text' => '服务亮点',
                'url' => route('spot.list')
            ], ['text' => $header])->row($tab);
    }

    public function editAction($request, $id = 0, $isCreate = false)
    {
        if ($isCreate) {
            $data = [];
            $form = new Form($data);
        } else {
            $data = DesignerSpot::get('*', ['spot_id' => $id]);
            $form = new Form($data);
            $form->text('spot_id', '亮点ID')->setWidth(4, 2)->readOnly();
        }

        $form->text('spot_name', '亮点名称')->setWidth(4, 2);
        $form->select('spot_status', '亮点状态')
            ->setWidth(4, 2)
            ->options(DesignerSpot::STATUS_POOL)
            ->default(DesignerSpot::STATUS_NORMAL);

        $form->action('/admin/Designer/service_spot/save');
        return $form->render();
    }

    // 编辑奖项
    public function editSubmit(Request $request)
    {
        $request = $request->all();
        $data = [
            'spot_name' => $request['spot_name'],
            'spot_status' => $request['spot_status']
        ];
        if (isset($request['spot_id'])) {
            // 如果已经有设计师设置了该亮点，不可修改
            if ($request['spot_status'] && Designer::getNumBySpotId($request['spot_id'])) {
                $error = new MessageBag(['title' => '已有设计师']);
                return back()->with(compact('error'));
            }
            $rs = DesignerSpot::updateInfo($request['spot_id'], $data);
        } else {
            if (DesignerSpot::getByCondition(['spot_name' => $request['spot_name']])) {
                $error = new MessageBag(['title' => '已经存在该亮点']);
                return back()->with(compact('error'));
            }
            $rs = DesignerSpot::create($data);
        }
        $title = isset($request['spot_id']) ? '修改成功' : '添加成功';
        if (!$rs) {
            $error = new MessageBag(['title' => $title]);
            return back()->with(compact('error'));
        }
        $success = new MessageBag(['title' => $title]);
        return redirect('/admin/Designer/service_spot')->with(compact('success'));
    }
}
